﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;

public partial class ClientRegistration : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Admin admin;
    List<Admin> admins;
    Client client;
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        { }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        client = new Client(_dataObject);
        client.AdminId = 3;
        client.FirstName = txtFirstName.Text;
        client.LastName = txtLastName.Text;
        client.Phone = txtPhone.Text;
        client.Email = txtEmailAddress.Text;
        client.Address = txtAddress.Text;
        client.City = txtCity.Text;
        client.State = txtState.Text;
        client.Zip = txtPincode.Text;
        client.Username = txtUsername.Text;
        client.Password = txtPassword.Text;
        client.CommissionRate = "0";
        client.NECommission = "0";
        client.Limit = 25000;
        client.MatchTempLimit = 25000;
        client.ActualLimit = 25000;
        client.MinLimit = 1000;
        client.MaxLimit = 50000;
        client.IsLogin = false;
        //client.IsActive = true;
        //client.IsDeleted = false;
        if (rdoActive.SelectedValue == "1")
        {
            client.IsActive = true;
            client.IsDeleted = false;
        }
        else
        {
            client.IsActive = false;
            client.IsDeleted = true;
        }
        bool userNameExist = CheckUserNameExist(txtUsername.Text);
        if (Request.QueryString["Id"] != null)
        {
            if (!userNameExist)
            {
                client.Id = Convert.ToInt32(Request.QueryString["Id"].ToString());
                client.DaeUpdated = System.DateTime.UtcNow;
                client.Update();
                clearControl();
                Response.Redirect("Login.aspx");

                //layerSuccess.InnerHtml = "<strong>Success!</strong> Client updated successfully.";
                //layerSuccess.Attributes.Add("style", "display:block;");
                //layerError.Attributes.Add("style", "display:none;");
            }
            else
            {
                layerError.InnerHtml = "<strong>Error!</strong> Email already exist."; ;
                layerError.Attributes.Add("style", "display:block;");
                layerSuccess.Attributes.Add("style", "display:none;");
            }
        }
        else
        {
            if (!userNameExist)
            {
                client.DateAdded = System.DateTime.UtcNow;
                client.Create();
                clearControl();
                layerSuccess.InnerHtml = "<strong>Success!</strong> Client added successfully.";
                layerSuccess.Attributes.Add("style", "display:block;");
                layerError.Attributes.Add("style", "display:none;");
                Response.Redirect("Login.aspx");
            }
            else
            {
                layerError.InnerHtml = "<strong>Exist!</strong> Username already exist.";
                layerError.Attributes.Add("style", "display:block;");
                layerSuccess.Attributes.Add("style", "display:none;");
            }
        }


    }
    private bool CheckUserNameExist(string username)
    {
        bool isExist = false;
        int ClientId = 0;
        if (Convert.ToString(Request.QueryString["Id"]) != "" && Request.QueryString["Id"] != null)
        {
            ClientId = Convert.ToInt32(Request.QueryString["Id"]);
        }
        List<Client> clients = new List<Client>();
        clients = client.CheckUserNameExist(ClientId, username);
        if (clients.Count > 0)
        {
            isExist = true;
        }
        else
        {
            isExist = false;
        }
        return isExist;
    }

    private void clearControl()
    {
        txtFirstName.Text = string.Empty;
        txtLastName.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtEmailAddress.Text = string.Empty;
        txtAddress.Text = string.Empty;
        txtCity.Text = string.Empty;
        txtState.Text = string.Empty;
        txtPincode.Text = string.Empty; ;
        txtUsername.Text = string.Empty;
        txtPassword.Text = string.Empty;
        
    }
}